package hot100;

import java.util.PriorityQueue;

public class commontest {

    public int minOperations(int[] nums, int k) {
        if (nums.length == 1) {
            return 0;
        }
        PriorityQueue<Long> minHeap = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            minHeap.add(Long.valueOf(nums[i]));
        }
        int count = 0;
        while (minHeap.size() >= 2) {
            if (minHeap.peek() >= k) {
                break;
            }
            long a = minHeap.poll();
            long b = minHeap.poll();
            minHeap.offer(Math.min(a, b) * 2 + Math.max(a, b));
            count++;
        }
        return count;
    }

    public int lengthOfLastWord(String s) {
        int length = s.length();
        int res = 0;
        for (int i = length - 1; i >= 0; i--) {
            if (s.charAt(i) != ' ') {
                res++;
            } else if (res > 0) {
                break;
            }
        }
        return res;
    }

    public String longestCommonPrefix(String[] strs) {
        String res = "";
        int len = 200;
        for (String str : strs) {
            len = Math.min(len, str.length());
        }
        for (int i = 0; i < len; i++) {
            char comparison = strs[0].charAt(i);
            for (String str : strs) {
                if (comparison != str.charAt(i)) {
                    return res;
                }
            }
            res = res + comparison;
        }
        return res;
    }
}
